import 'package:efood_multivendor/controller/order_controller.dart';
import 'package:efood_multivendor/controller/product_controller.dart';
import 'package:efood_multivendor/data/model/body/review_body.dart';
import 'package:efood_multivendor/data/model/response/order_details_model.dart';
import 'package:efood_multivendor/data/model/response/product_model.dart';
import 'package:efood_multivendor/helper/price_converter.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/custom_button.dart';
import 'package:efood_multivendor/view/base/custom_image.dart';
import 'package:efood_multivendor/view/base/custom_snackbar.dart';
import 'package:efood_multivendor/view/base/my_text_field.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class ProductReviewWidget extends StatefulWidget {
  List<bool> isSubmitted = [];

  @override
  State<ProductReviewWidget> createState() => _ProductReviewWidgetState();
}

class _ProductReviewWidgetState extends State<ProductReviewWidget> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<OrderController>(builder: (orderController) {
      return GetBuilder<ProductController>(builder: (productController) {
        List<Details> newListOfFoods = [];
        if (orderController.orderDetails != null) {
          newListOfFoods = orderController.orderDetails!.details
                  ?.where((detail) => detail.reviewed != true)
                  .toList() ??
              [];
        }
        return Scrollbar(
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: orderController.orderDetails == null &&
                    productController.unreviewedOrdersResponse != null
                ? Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                        SizedBox(
                          height: MediaQuery.of(context).size.height / 4,
                        ),
                        CircularProgressIndicator()
                      ])
                : newListOfFoods.isEmpty
                    ? Column(
                        children: [
                          SizedBox(
                            height: MediaQuery.of(context).size.height / 4,
                          ),
                          Center(
                            child: Text('All items are reviewed'),
                          ),
                        ],
                      )
                    : Column(
                        children: [
                          Padding(
                            padding: EdgeInsets.all(10),
                            child: Container(
                              child: Column(
                                children: [
                                  Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                                    children: [
                                      Text('Restaurant Name:'),
                                      SizedBox(width: 10),
                                      Text(
                                        orderController.orderDetails?.restaurant
                                                ?.name ??
                                            '',
                                        style: TextStyle(
                                            color: Theme.of(context)
                                                .colorScheme
                                                .primary),
                                      ),
                                    ],
                                  ),
                                ],
                              ),
                            ),
                          ),
                          Center(
                            child: SizedBox(
                              width: Dimensions.WEB_MAX_WIDTH,
                              child: ListView.builder(
                                itemCount: newListOfFoods.length,
                                physics: NeverScrollableScrollPhysics(),
                                shrinkWrap: true,
                                padding: EdgeInsets.all(
                                    Dimensions.PADDING_SIZE_SMALL),
                                itemBuilder: (context, index) {
                                  return Column(
                                    children: [
                                      Container(
                                        padding: EdgeInsets.all(
                                            Dimensions.PADDING_SIZE_SMALL),
                                        margin: EdgeInsets.only(
                                            bottom:
                                                Dimensions.PADDING_SIZE_SMALL),
                                        decoration: BoxDecoration(
                                          boxShadow: [
                                            BoxShadow(
                                              color:
                                                  Colors.grey.withOpacity(0.2),
                                              spreadRadius: 1,
                                              blurRadius: 2,
                                              offset: Offset(0, 1),
                                            ),
                                          ],
                                          color: Theme.of(context).cardColor,
                                          borderRadius: BorderRadius.circular(
                                              Dimensions.PADDING_SIZE_SMALL),
                                        ),
                                        child: Column(
                                          children: [
                                            Row(
                                              children: [
                                                ClipRRect(
                                                  borderRadius:
                                                      BorderRadius.circular(
                                                          Dimensions
                                                              .RADIUS_SMALL),
                                                  child: CustomImage(
                                                    height: 70,
                                                    width: 85,
                                                    fit: BoxFit.cover,
                                                    image: newListOfFoods[index]
                                                        .foodDetails!
                                                        .image!,
                                                  ),
                                                ),
                                                SizedBox(
                                                    width: Dimensions
                                                        .PADDING_SIZE_SMALL),
                                                Expanded(
                                                  child: Column(
                                                    crossAxisAlignment:
                                                        CrossAxisAlignment
                                                            .start,
                                                    mainAxisAlignment:
                                                        MainAxisAlignment
                                                            .center,
                                                    children: [
                                                      Text(
                                                        newListOfFoods[index]
                                                                .foodDetails
                                                                ?.name ??
                                                            '',
                                                        style: sfMedium,
                                                        maxLines: 2,
                                                        overflow: TextOverflow
                                                            .ellipsis,
                                                      ),
                                                      SizedBox(height: 10),
                                                      Text(
                                                        PriceConverter.convertPrice(
                                                            newListOfFoods[
                                                                        index]
                                                                    .foodDetails
                                                                    ?.price ??
                                                                0),
                                                        style: sfBold,
                                                      ),
                                                    ],
                                                  ),
                                                ),
                                                Row(
                                                  children: [
                                                    Text(
                                                      '${'quantity'.tr}: ',
                                                      style: sfMedium.copyWith(
                                                        color: Theme.of(context)
                                                            .disabledColor,
                                                      ),
                                                      overflow:
                                                          TextOverflow.ellipsis,
                                                    ),
                                                    Text(
                                                      newListOfFoods[index]
                                                          .quantity
                                                          .toString(),
                                                      style: sfMedium.copyWith(
                                                        color: Theme.of(context)
                                                            .primaryColor,
                                                        fontSize: Dimensions
                                                            .fontSizeSmall,
                                                      ),
                                                      overflow:
                                                          TextOverflow.ellipsis,
                                                    ),
                                                  ],
                                                ),
                                              ],
                                            ),
                                            Divider(height: 20),
                                            Text(
                                              'rate_the_food'.tr,
                                              style: sfMedium.copyWith(
                                                color: Theme.of(context)
                                                    .disabledColor,
                                              ),
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                            SizedBox(
                                                height: Dimensions
                                                    .PADDING_SIZE_SMALL),
                                            SizedBox(
                                              height: 30,
                                              child: ListView.builder(
                                                itemCount: 5,
                                                shrinkWrap: true,
                                                scrollDirection:
                                                    Axis.horizontal,
                                                itemBuilder: (context, i) {
                                                  // print(
                                                  //     '=====================================');
                                                  // print(productController
                                                  //     .ratingList[index]);
                                                  return InkWell(
                                                    child: Icon(
                                                      productController
                                                                      .ratingList[
                                                                  index] <
                                                              (i + 1)
                                                          ? Icons.star_border
                                                          : Icons.star,
                                                      size: 25,
                                                      color: productController
                                                                      .ratingList[
                                                                  index] <
                                                              (i + 1)
                                                          ? Theme.of(context)
                                                              .disabledColor
                                                          : Theme.of(context)
                                                              .colorScheme
                                                              .secondary,
                                                    ),
                                                    onTap: () {
                                                      productController
                                                          .setRating(
                                                              index, i + 1);
                                                    },
                                                  );
                                                },
                                              ),
                                            ),
                                            SizedBox(
                                                height: Dimensions
                                                    .PADDING_SIZE_LARGE),
                                            Text(
                                              'share_your_opinion'.tr,
                                              style: sfMedium.copyWith(
                                                color: Theme.of(context)
                                                    .disabledColor,
                                              ),
                                              overflow: TextOverflow.ellipsis,
                                            ),
                                            SizedBox(
                                                height: Dimensions
                                                    .PADDING_SIZE_LARGE),
                                            MyTextField(
                                              maxLines: 3,
                                              capitalization:
                                                  TextCapitalization.sentences,
                                              isEnabled: true,
                                              hintText:
                                                  'write_your_review_here'.tr,
                                              fillColor: Theme.of(context)
                                                  .disabledColor
                                                  .withOpacity(0.05),
                                              onChanged: (text) =>
                                                  productController.setReview(
                                                      index, text),
                                            ),
                                            SizedBox(height: 20),
                                            Padding(
                                              padding: EdgeInsets.symmetric(
                                                  horizontal: Dimensions
                                                      .PADDING_SIZE_LARGE),
                                              child: !productController
                                                      .loadingList[index]
                                                  ? CustomButton(
                                                      buttonText: 'submit'.tr,
                                                      onPressed: () {
                                                        if (productController
                                                                    .ratingList[
                                                                index] ==
                                                            0) {
                                                          showCustomSnackBar(
                                                              'give_a_rating'
                                                                  .tr);
                                                        } else {
                                                          FocusScopeNode
                                                              currentFocus =
                                                              FocusScope.of(
                                                                  context);
                                                          if (!currentFocus
                                                              .hasPrimaryFocus) {
                                                            currentFocus
                                                                .unfocus();
                                                          }
                                                          ReviewBody
                                                              reviewBody =
                                                              ReviewBody(
                                                            productId:
                                                                newListOfFoods[
                                                                        index]
                                                                    .foodDetails
                                                                    ?.id
                                                                    .toString(),
                                                            rating:
                                                                productController
                                                                    .ratingList[
                                                                        index]
                                                                    .toString(),
                                                            comment: productController
                                                                    .reviewList[
                                                                        index]
                                                                    .isEmpty
                                                                ? 'No comment'
                                                                : productController
                                                                        .reviewList[
                                                                    index],
                                                            orderId:
                                                                newListOfFoods[
                                                                        index]
                                                                    .orderId
                                                                    .toString(),
                                                          );
                                                          productController
                                                              .submitReview(
                                                                  index,
                                                                  reviewBody)
                                                              .then((value) {
                                                            // print(reviewBody
                                                            //     .toJson()
                                                            //     .toString());
                                                            if (value
                                                                .isSuccess) {
                                                              showCustomSnackBar(
                                                                  value.message,
                                                                  isError:
                                                                      false);
                                                              productController
                                                                  .setReview(
                                                                      index,
                                                                      '');
                                                              orderController.getOrderDetails(
                                                                  orderController
                                                                      .orderDetails
                                                                      ?.id
                                                                      .toString());
                                                              productController
                                                                  .setRating(
                                                                      index, 0);
                                                            } else {
                                                              showCustomSnackBar(
                                                                  value
                                                                      .message);
                                                            }
                                                          });
                                                        }
                                                      },
                                                    )
                                                  : Center(
                                                      child:
                                                          CircularProgressIndicator
                                                              .adaptive(
                                                        backgroundColor:
                                                            Theme.of(context)
                                                                .colorScheme
                                                                .secondary,
                                                      ),
                                                    ),
                                            ),
                                          ],
                                        ),
                                      ),
                                    ],
                                  );
                                },
                              ),
                            ),
                          ),
                        ],
                      ),
          ),
        );
      });
    });
  }
}
